#include "ExaminableObject.h"

#include <iostream>
#include <iomanip>

ExaminableObject::ExaminableObject(	btScalar mass, 
                                        btMotionState *motionState, 
                                        btCollisionShape *collisionShape, 
                                        const btVector3 &localInertia) : btRigidBody(mass, motionState, collisionShape, localInertia) {
  _oldPos = get_physic_position();
}

ExaminableObject::~ExaminableObject(void) {
  btRigidBody::~btRigidBody();
  _visibleObject = NULL;
}

btVector3 ExaminableObject::get_physic_position(void) {
	btVector3 pos = getWorldTransform().getOrigin();
  return pos;
}

void ExaminableObject::set_physic_position(btVector3& pos) {
	getWorldTransform().setOrigin(pos);
}

void ExaminableObject::set_visible_object(Object* obj) {
  _visibleObject = obj;
  _selected = false;
  _forceUpdate = false;
}

Object* ExaminableObject::get_visible_object(void) {
  return _visibleObject;
}

void ExaminableObject::set_selected(bool s) {
  _oldPos.setY(-1000.0f);
  _selected = s;
}

void ExaminableObject::set_scale(float s) {
	_scaleFactor = s;
	_visibleObject->scale_to(s);
	_forceUpdate = true;
}

void ExaminableObject::scale_up(float inc) {
	_scaleFactor += inc;
	if (_scaleFactor > MAX_SCALE_FACTOR) _scaleFactor = MAX_SCALE_FACTOR;
	set_scale(_scaleFactor);
}

void ExaminableObject::scale_down(float inc) {
	_scaleFactor -= inc;
	if (_scaleFactor < MIN_SCALE_FACTOR) _scaleFactor = MIN_SCALE_FACTOR;
	set_scale(_scaleFactor);
}

void ExaminableObject::update(void) {
	btVector3 currentPos = get_physic_position();
    float dist2 = currentPos.distance2(_oldPos);
    if ((dist2 > MINIMUM_STEP) || _forceUpdate) {
		_visibleObject->set_centre (currentPos.x(), currentPos.y(), currentPos.z());
		_oldPos = currentPos;
		_forceUpdate = false;
	}
}
